import { getCollect, getDetail } from "../../api/introduct"

// pages/introduct/introduct.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: null,
    duration: 0,
    isCollect: false,
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(option) {
    // console.log(option.courseId);
    const res = await getDetail(option.courseId)
    console.log(res);
    let duration = 0;
    for(var i = 0; i < res.data.sections.length; i++ ){
        for(var j = 0; j < res.data.sections[i].subSections.length; j++) {
            duration += res.data.sections[i].subSections[j].file.duration;
        }
    }
    // console.log(Math.ceil(duration/60));
    this.setData({dataList: res.data,duration: Math.ceil(duration/60)})
    this.setData({isCollect: res.isCollect})
  },

  // 收藏按钮
  onCollect: async function(data){
    let id = data.target.dataset.id;
    const that = this;
    wx.showModal({
        content: this.data.isCollect ? '取消收藏' : '确定收藏',
        success: async function (res) {
          if (res.confirm) {
            const res = await getCollect(id)
            console.log(res);
            if(res.code == 0){
                wx.showToast({
                    title: '操作成功',
                    icon: 'success',
                    duration: 1000
                })
            }
            that.setData({isCollect: !that.data.isCollect})
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }  
      })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    let id = res.target.dataset.id;
    return {
        title: res.target.dataset.title,
        path: `/pages/article/detail/detail?id=${id}`
    };
  }
})